package cc.ucanuup.config.atom;

import java.sql.SQLException;
import java.util.HashMap;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;

import cc.ucanuup.config.atom.dbsource.BlogDatasourceProperties;

@Configuration
@DependsOn("transactionManager")
@EnableJpaRepositories(basePackages = {"cc.ucanuup.service.base.dao","cc.ucanuup.service.blog.dao"}, entityManagerFactoryRef = "srmEntityManager", transactionManagerRef = "transactionManager")
@EnableConfigurationProperties(BlogDatasourceProperties.class)
public class BlogConfig {

	@Autowired
	private JpaVendorAdapter jpaVendorAdapter;

	@Autowired
	private BlogDatasourceProperties srmDatasourceProperties;

	@Primary
	@Bean(name="srmDataSource", initMethod = "init", destroyMethod = "close")
	@Qualifier("srmDataSource")
	public DataSource srmDataSource() throws SQLException {
		System.out.println("mysql Blog:"+srmDatasourceProperties.getUrl());
		System.out.println("mysql Blog:"+srmDatasourceProperties.getUsername());
		System.out.println("mysql Blog:"+srmDatasourceProperties.getPassword());
		MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
		mysqlXaDataSource.setURL(srmDatasourceProperties.getUrl());
		mysqlXaDataSource.setUser(srmDatasourceProperties.getUsername());
		mysqlXaDataSource.setPassword(srmDatasourceProperties.getPassword());
		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

		AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
		xaDataSource.setXaDataSource(mysqlXaDataSource);
		xaDataSource.setUniqueResourceName("datasourceSrm");
		xaDataSource.setMaxPoolSize(20);
		/*Properties p = new Properties();
		p.setProperty("pinGlobalTxToPhysicalConnection", "true");
		xaDataSource.setXaProperties(p);*/

		//		xaDataSource.setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");
		return xaDataSource;
	}

	@Primary
	@Bean(name = "srmEntityManager")
	@DependsOn("transactionManager")
	public LocalContainerEntityManagerFactoryBean customerEntityManager() throws Throwable {

		HashMap<String, Object> properties = new HashMap<String, Object>();
		properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());
		properties.put("javax.persistence.transactionType", "JTA");
		//properties.put("hibernate.default_schema", srmDatasourceProperties.getDefault_schema());

		LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
		entityManager.setJtaDataSource(srmDataSource());
		entityManager.setJpaVendorAdapter(jpaVendorAdapter);
		entityManager.setPackagesToScan("cc.ucanuup.service.base.entity","cc.ucanuup.service.blog.entity");
		entityManager.setPersistenceUnitName("customerPersistenceUnit");
		entityManager.setJpaPropertyMap(properties);
		return entityManager;
	}

}
